/*
 * scheduler_o_n.h
 *
 *  Created on: 2011-5-19
 *      Author: chenl
 */

#ifndef SCHEDULER_O_N_H_
#define SCHEDULER_O_N_H_

#include "scheduler.h"
#include "task_wrapper.h"
#include "global.h"
#include "common/common_type.h"
#include "processmgt.h"

class CScheduler_O_N: public IScheduler
{
public:
	CScheduler_O_N();
	virtual ~CScheduler_O_N();

	virtual int32_t Initialize()
	{
		return S_OK;
	}
	virtual int32_t Uninitialize()
	{
		return S_OK;
	}
	virtual void schedule();
	virtual void scheduler_tick()
	{
		CProcessMgt& processMgt = GET_PROCESSMGT_INSTANCE();
		CTask* pTask = processMgt.CurrentTask();
		if (NULL == pTask)
		{
			logEngine.WriteLog(enmLogLevel_ERROR,
					"Get current task failed when CPSApp::UpDateTaskTimes()!");
			return;
		}
		pTask->UpdateTaskTimes();
	}
protected:
	virtual inline int goodness(struct task_struct& task)
	{
		int weight; // 权值，作为衡量进程是否运行的唯一依据

		weight = -1;
		struct task_struct* p = &task;
		if (p->policy & SCHED_YIELD)
			goto out; //如果该进程愿意“礼让（yield）”，则让其权值为－1
		switch (p->policy)
		{
		/* 实时进程*/
		case SCHED_FIFO:
		case SCHED_RR:

			/* 普通进程 */
		case SCHED_OTHER:
			weight = p->counter;
			if (!weight)
				goto out;
			weight += 20 - p->nice;

		}
		out: return weight; /*返回权值*/
	}
	virtual inline void switch_to(CTask& prevTask, CTask& nextTask)
	{
		GET_PROCESSMGT_INSTANCE().SetCurrentTask(nextTask);
	}

public:
	CPSThreadLog& logEngine;
};

#endif /* SCHEDULER_O_N_H_ */
